package com.huawei.demo;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.das.v3.DasClient;
import com.huaweicloud.sdk.das.v3.model.ChangeSqlSwitchBody;
import com.huaweicloud.sdk.das.v3.model.ChangeSqlSwitchRequest;
import com.huaweicloud.sdk.das.v3.model.ChangeSqlSwitchResponse;
import com.huaweicloud.sdk.das.v3.model.ExportSlowQueryLogsRequest;
import com.huaweicloud.sdk.das.v3.model.ExportSlowQueryLogsResponse;
import com.huaweicloud.sdk.das.v3.model.ExportSqlStatementsRequest;
import com.huaweicloud.sdk.das.v3.model.ExportSqlStatementsResponse;
import com.huaweicloud.sdk.das.v3.model.ShowSqlSwitchStatusRequest;
import com.huaweicloud.sdk.das.v3.model.ShowSqlSwitchStatusResponse;
import com.huaweicloud.sdk.das.v3.region.DasRegion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DataBaseLogDemo {
    private static final Logger logger = LoggerFactory.getLogger(DataBaseLogDemo.class.getName());

    /**
     * args[0] = <<YOUR AK>>
     * args[1] = <<YOUR SK>>
     * args[2] = <<YOUR REGION_ID>>
     * args[3] = <<YOUR INSTANCE_ID>>
     * args[4] = <<YOUR DATASTORE_TYPE>>
     * args[5] = <<SWITCH_TYPE>>
     * args[6] = <<SWITCH_STATUS>>
     * args[7] = <<DATA_EXPORT_START_AT>>
     * args[8] = <<DATA_EXPORT_END_AT>>
     */
    public static void main(String[] args) {
        String ak = args[0];
        String sk = args[1];
        String regionId = args[2];
        String instanceId = args[3];
        String datastoreType = args[4];
        String type = args[5];
        Integer status = Integer.valueOf(args[6]);
        Long startAt = Long.valueOf(args[7]);
        Long endAt = Long.valueOf(args[8]);

        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);
        DasClient client = DasClient.newBuilder()
                .withCredential(auth)
                .withRegion(DasRegion.valueOf(regionId))
                .build();

        // 查询全量SQL和慢SQL的开关状态
        showSqlSwitchStatus(client, instanceId, type, datastoreType);

        // 开启/关闭全量SQL、慢SQL开关
        changeSqlSwitch(client, instanceId, type, status, datastoreType);

        // 导出慢SQL数据
        exportSlowQueryLogs(client, instanceId, datastoreType, startAt, endAt);

        // 导出全量SQL
        exportSqlStatements(client, instanceId, datastoreType, startAt, endAt);
    }

    private static void showSqlSwitchStatus(DasClient client, String instanceId, String type, String datastoreType) {
        ShowSqlSwitchStatusRequest request = new ShowSqlSwitchStatusRequest();
        request.withInstanceId(instanceId);
        request.withType(type);
        request.withDatastoreType(datastoreType);

        invokeClient(() -> {
            ShowSqlSwitchStatusResponse response = client.showSqlSwitchStatus(request);
            logger.info(response.toString());
        });
    }

    private static void changeSqlSwitch(DasClient client, String instanceId, String type, Integer status,
            String datastoreType) {
        ChangeSqlSwitchRequest request = new ChangeSqlSwitchRequest();
        ChangeSqlSwitchBody switchBody = new ChangeSqlSwitchBody()
                .withType(type)
                .withStatus(status)
                .withDatastoreType(datastoreType);
        request.withInstanceId(instanceId);
        request.withBody(switchBody);

        invokeClient(() -> {
            ChangeSqlSwitchResponse response = client.changeSqlSwitch(request);
            logger.info(response.toString());
        });
    }

    private static void exportSlowQueryLogs(DasClient client, String instanceId, String datastoreType, Long startAt,
            Long endAt) {
        ExportSlowQueryLogsRequest request = new ExportSlowQueryLogsRequest();
        request.withInstanceId(instanceId);
        request.withDatastoreType(datastoreType);
        request.withStartAt(startAt);
        request.withEndAt(endAt);
        request.withLimit(10);

        invokeClient(() -> {
            ExportSlowQueryLogsResponse response = client.exportSlowQueryLogs(request);
            logger.info(response.toString());
        });
    }

    private static void exportSqlStatements(DasClient client, String instanceId, String datastoreType, Long startAt,
            Long endAt) {
        ExportSqlStatementsRequest request = new ExportSqlStatementsRequest();
        request.withInstanceId(instanceId);
        request.withDatastoreType(datastoreType);
        request.withStartAt(startAt);
        request.withEndAt(endAt);
        request.withLimit(10);

        invokeClient(() -> {
            ExportSqlStatementsResponse response = client.exportSqlStatements(request);
            logger.info(response.toString());
        });
    }

    private static <T> void invokeClient(Runnable runnable) {
        try {
            runnable.run();
        } catch (ConnectionException e) {
            logger.error("ConnectionException", e);
        } catch (RequestTimeoutException e) {
            logger.error("RequestTimeoutException", e);
        } catch (ServiceResponseException e) {
            logger.error("httpStatusCode: {}, errorCode: {}, errorMsg: {}", e.getHttpStatusCode(), e.getErrorCode(),
                    e.getErrorMsg());
        }
    }
}
